﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using IGGCore;

namespace IGGWindowApp.Panels
{
    public partial class PanelScoreboard : UserControl
    {
        private List<Panel> PanelsTitle;
        private List<Panel> PanelsTeamHome;
        private List<Panel> PanelsTeamAway;

        public PanelScoreboard()
        {
            InitializeComponent();
            PanelsTitle = new List<Panel>();
            PanelsTeamHome = new List<Panel>();
            PanelsTeamAway = new List<Panel>();
            GenerateLayout();
        }

        private void GenerateLayout()
        {
            // How Many Innings Can I Show?
            // Check Size of current Parent Panel

            Color InningTitleColor = Color.Gray;

            int xpos = 324;
            int ypos = 28;
            int awayypos = ypos + 32;
            int homeypos = awayypos + 30;
            
            int xposIncr = 34;
            for (int i = 1; i < 12; i++)
            {
                if (i < 10)
                {
                    PanelsTitle.Add(CreatePanel(xpos, ypos, i.ToString(), InningTitleColor));
                }
                else
                {
                    PanelsTitle.Add(CreatePanel(xpos, ypos, "", InningTitleColor));
                }
                PanelsTeamAway.Add(CreatePanel(xpos, awayypos, " ", Color.LightGray));
                PanelsTeamHome.Add(CreatePanel(xpos, homeypos, " ", Color.LightGray));
                xpos += xposIncr;
            }

            PanelsTitle.Add(CreatePanel(xpos, ypos, " ", InningTitleColor));
            PanelsTeamAway.Add(CreatePanel(xpos, awayypos, " ", Color.LightGray));
            PanelsTeamHome.Add(CreatePanel(xpos, homeypos, " ", Color.LightGray));
            xpos += xposIncr;

            PanelsTitle.Add(CreatePanel(xpos, ypos, "R", InningTitleColor));
            PanelsTeamAway.Add(CreatePanel(xpos, awayypos, " ", Color.LightGray));
            PanelsTeamHome.Add(CreatePanel(xpos, homeypos, " ", Color.LightGray));
            xpos += xposIncr;

            PanelsTitle.Add(CreatePanel(xpos, ypos, "H", InningTitleColor));
            PanelsTeamAway.Add(CreatePanel(xpos, awayypos, " ", Color.LightGray));
            PanelsTeamHome.Add(CreatePanel(xpos, homeypos, " ", Color.LightGray));
            xpos += xposIncr;

            PanelsTitle.Add(CreatePanel(xpos, ypos, "E", InningTitleColor));
            PanelsTeamAway.Add(CreatePanel(xpos, awayypos, " ", Color.LightGray));
            PanelsTeamHome.Add(CreatePanel(xpos, homeypos, " ", Color.LightGray));
            xpos += xposIncr;

            PanelsTitle.Add(CreatePanel(xpos, ypos, " ", InningTitleColor));
            PanelsTeamAway.Add(CreatePanel(xpos, awayypos, " ", Color.LightGray));
            PanelsTeamHome.Add(CreatePanel(xpos, homeypos, " ", Color.LightGray));
            xpos += xposIncr;

            for (int i = 1; i < 4; i++)
            {
                PanelsTitle.Add(CreatePanel(xpos, ypos, "S", InningTitleColor));
                PanelsTeamAway.Add(CreatePanel(xpos, awayypos, " ", Color.LightGray));
                PanelsTeamHome.Add(CreatePanel(xpos, homeypos, " ", Color.LightGray));
                xpos += xposIncr;
            }

            // Init Titles. Extra Innings are not Visible

            //
        }

        private Panel CreatePanel(int x, int y, string labelStr, Color backColor)
        {
            Label label = new Label();
            label.Dock = System.Windows.Forms.DockStyle.Fill;
            label.Font = new System.Drawing.Font("Segoe UI", 12F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            label.Location = new System.Drawing.Point(0, 0);
            label.Size = new System.Drawing.Size(30, 22);
            label.Text = labelStr;
            label.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;

            Panel panel = new Panel();
            panel.BackColor = backColor;
            panel.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
            panel.Controls.Add(label);
            panel.Location = new System.Drawing.Point(x, y);
            panel.Size = new System.Drawing.Size(32, 24);

            this.Controls.Add(panel);
            return panel;
        }



        public void SetData(Game game)
        {
            for (int i = 1; i < PanelsTeamAway.Count; i++)
            {
                PanelsTeamAway[i].ResetText();
                PanelsTeamHome[i].ResetText();
            }

            lblTeamAway.Text = game.AwayTeam.City;
            lblTeamHome.Text = game.HomeTeam.City;
            pctTeamAway.Image = listLogos.Images[game.AwayTeam.City];
            pctTeamHome.Image = listLogos.Images[game.HomeTeam.City];

            if (game.Status.Indicator.IsInProgress || game.Status.Indicator.IsTerminated || game.Status.Indicator.IsInterrupted)
            {
                for (int i = 1; i <= game.Status.Inning; i++)
                {
                    if (game.Linescore.Innings[i - 1].Away > -1)
                    {
                        PanelsTeamAway[i - 1].Controls[0].Text = game.Linescore.Innings[i - 1].Away.ToString();
                    }
                    if (i != game.Status.Inning || game.Status.InningTop == false)
                    {
                        if (game.Linescore.Innings[i - 1].Home > -1)
                        {
                            PanelsTeamHome[i - 1].Controls[0].Text = game.Linescore.Innings[i - 1].Home.ToString();
                        }
                    }
                }
            }

            if (game.Linescore != null)
            {
                PanelsTeamAway[12].Controls[0].Text = game.Linescore.GetAwayLineScore().Runs.ToString();
                PanelsTeamHome[12].Controls[0].Text = game.Linescore.GetHomeLineScore().Runs.ToString();

                PanelsTeamAway[13].Controls[0].Text = game.Linescore.GetAwayLineScore().Hits.ToString();
                PanelsTeamHome[13].Controls[0].Text = game.Linescore.GetHomeLineScore().Hits.ToString();

                PanelsTeamAway[14].Controls[0].Text = game.Linescore.GetAwayLineScore().Errors.ToString();
                PanelsTeamHome[14].Controls[0].Text = game.Linescore.GetHomeLineScore().Errors.ToString();
            }
            else
            {
                PanelsTeamAway[12].Controls[0].Text = "0";
                PanelsTeamHome[12].Controls[0].Text = "0";

                PanelsTeamAway[13].Controls[0].Text = "0";
                PanelsTeamHome[13].Controls[0].Text = "0";

                PanelsTeamAway[14].Controls[0].Text = "0";
                PanelsTeamHome[14].Controls[0].Text = "0";
            }
        }
    }
}
